/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.interaction.multitouch.client;

import java.util.ArrayList;
import java.util.List;

import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.interaction.InteractionHandlerManager;
import org.soupframework.gwt.designerpanel.client.interaction.impl.BaseContainerInteractionHandler;

import com.lombardi.mobilesafari.event.HasTouchHandlers;
import com.lombardi.mobilesafari.event.Touch;
import com.lombardi.mobilesafari.event.TouchCancelEvent;
import com.lombardi.mobilesafari.event.TouchCancelHandler;
import com.lombardi.mobilesafari.event.TouchEndEvent;
import com.lombardi.mobilesafari.event.TouchEndHandler;
import com.lombardi.mobilesafari.event.TouchMoveEvent;
import com.lombardi.mobilesafari.event.TouchMoveHandler;
import com.lombardi.mobilesafari.event.TouchStartEvent;
import com.lombardi.mobilesafari.event.TouchStartHandler;

public class DesignerPanelMultitouchInteractionHandler<T, CD extends ContainerDesigner<T>> extends BaseContainerInteractionHandler<T, CD> implements TouchStartHandler, TouchMoveHandler, TouchEndHandler, TouchCancelHandler {

	public DesignerPanelMultitouchInteractionHandler(InteractionHandlerManager<T> manager, CD designer){
		super(manager, designer);
	}

	@Override
	public void registerHandlersOnTarget(){
		if(!(this.getDesigner().getObjectView() instanceof HasTouchHandlers)){
			throw new IllegalArgumentException("Target widget must implement HasTouchHandlers interface");
		}

		((HasTouchHandlers)this.getDesigner().getObjectView()).addTouchStartHandler(this);
	}

	@Override
	public void onTouchStart(TouchStartEvent event) {
		List<T> selectedObjects = new ArrayList<T>();
		for(int i = 0 ; i < event.changedTouches().length() ; i ++){
			Touch touch = event.changedTouches().get(i);
			for(T object : this.getDesigner().getObjectsAtCoords(touch.getClientX(), touch.getClientY())){
				selectedObjects.add(object);
			}
		}
		if(event.targetTouches().length() > 1) {
			this.getDesigner().addObjectsToSelection(selectedObjects);
		}
		else if(event.targetTouches().length() == 1){
			if(selectedObjects.size() > 0){
				this.getDesigner().setSelection(selectedObjects);
			}
			else{
				this.getDesigner().clearSelection();
			}
		}
	}

	@Override
	public void onTouchMove(TouchMoveEvent event) {
	}

	@Override
	public void onTouchEnd(TouchEndEvent event) {
	}

	@Override
	public void onTouchCancel(TouchCancelEvent event) {
	}
}
